package com.cskaoyan.service;

import com.cskaoyan.bean.MarketSearchHistory;
import com.cskaoyan.bean.MarketSearchHistoryExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yi
 * @date 2022/10/13 18:42
 */
@Service
public class AdminHistoryServiceImpl implements AdminHistoryService{
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Override
    public CommonData<MarketSearchHistory> queryHistory(Integer userId, String keyword, BasePageInfo basePageInfo) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.setOrderByClause(basePageInfo.getSort());
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if(userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (keyword != null && !"".equals(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }

        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        PageInfo<MarketSearchHistory> marketSearchHistoryPageInfo = new PageInfo<>(marketSearchHistories);
        return CommonData.data(marketSearchHistoryPageInfo);
    }
}
